package cn.hsrell.hscommunity.service.impl;

import cn.hsrell.hscommunity.common.enums.CategoryTypeEnum;
import cn.hsrell.hscommunity.common.enums.IsDeletedFlagEnum;
import cn.hsrell.hscommunity.dao.SubjectLabelDao;
import cn.hsrell.hscommunity.entity.SubjectCategory;
import cn.hsrell.hscommunity.dao.SubjectCategoryDao;
import cn.hsrell.hscommunity.entity.SubjectLabel;
import cn.hsrell.hscommunity.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目分类(SubjectCategory)表服务实现类
 *
 * @author makejava
 * @since 2024-08-22 16:15:21
 */
@Service("subjectCategoryService")
@Slf4j
public class SubjectCategoryServiceImpl implements SubjectCategoryService {
    @Resource
    private SubjectCategoryDao subjectCategoryDao;

    @Resource
    private SubjectLabelDao subjectLabelDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SubjectCategory queryById(Long id) {
        return this.subjectCategoryDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param subjectCategory 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(SubjectCategory subjectCategory) {
        return this.subjectCategoryDao.insert(subjectCategory);
    }

    /**
     * 修改数据
     *
     * @param subjectCategory 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectCategory update(SubjectCategory subjectCategory) {
        this.subjectCategoryDao.update(subjectCategory);
        return this.queryById(subjectCategory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        //判断当前分类是一级分类还是二级分类
        SubjectCategory subjectCategory = subjectCategoryDao.queryById(id);
        if (subjectCategory.getCategoryType() == CategoryTypeEnum.PRIMARY.getCode()) {
            //当前为一级分类，判断该一级分类下是否有二级分类
            SubjectCategory category = new SubjectCategory();
            category.setParentId(id);
            category.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            List<SubjectCategory> subjectCategoryList = subjectCategoryDao.queryByCondition(id);
            if (subjectCategoryList.size() > 0) {
                //TODO 可以在此进行更为优雅的代码处理。例如多写几个contant
                log.info("当前分类下存在二级分类，不能删除");
                return false;
            }
            return this.subjectCategoryDao.deleteById(id) > 0;
        }else {
            //当前为二级分类，判断该二级分类下是否有标签
            SubjectLabel label = new SubjectLabel();
            label.setCategoryId(String.valueOf(id));
            label.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            List<SubjectLabel> subjectLabelList = subjectLabelDao.queryByCondition(label);
            if (subjectLabelList.size() > 0) {
                //TODO 可以在此进行更为优雅的代码处理。例如多写几个contant
                log.info("当前分类下存在标签，不能删除");
                return false;
            }
            return this.subjectCategoryDao.deleteById(id) > 0;
        }
    }
}
